<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      show-footer
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
      :loading="loading"
    >
      <vxe-table-column>
        <vxe-table-column field="licenseNumber" title="车牌号" min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.licenseNumber" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="classificationCode"
          title="车辆分类"
          :formatter="formatterVehicleClassification"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.classificationCode" placeholder="请选择" clearable>
            <el-option
              v-for="item in vehicleClassificationList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
              clearable
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="violationSum" title="违章条数" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.violationSumStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.violationSumEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="deductPointsSum" title="违章罚款合计" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.deductPointsSumStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.deductPointsSumEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="penaltyAmountSum" title="违章积分合计" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.penaltyAmountSumStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.penaltyAmountSumEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" width="50">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="code => showModel(scope.row.vehicleCode, 'Detail')">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelDetailFlag" title="详情" width="1200" height="600" resize>
      <compDetail ref="DetailRef" />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import compDetail from "./components/vehicleViolationDetail.vue";
import { vehicleViolation } from "@/api/vehicleService";
import { getListDict } from "@/api/commonApi";
export default {
  name: "VehicleViolationList",
  components: {
    compDetail
  },
  data() {
    return {
      loading: false,
      tableData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      modelDetailFlag: false,
      vehicleClassificationList: [],
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        total: 0
      }
    };
  },
  created() {
    // 获取数据字典： 车型，车长，车宽，车高，吨位，定位来源
    getListDict({
      types:
        "vehicleClassification,vehicleLength,vehicleWidth,vehicleHigh,vehicleTonnage,locationSource"
    }).then(response => {
      this.vehicleClassificationList = response.data.filter(
        item => item.type === "vehicleClassification"
      )[0]["dictList"];
      this.findList();
    });
  },
  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
      vehicleViolation(this.listQuery).then(response => {
        this.tableData = response.data.records;
        this.listQuery.total = response.data.total;
        this.loading = false;
        resolve();
      });
      })
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 翻译车型
    formatterVehicleClassification({ cellValue }) {
      const item = this.vehicleClassificationList.find(
        item => item.code === cellValue
      );
      return item ? item.name : "";
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 展示弹框
    showModel(code, prefix) {
      if (code) {
        this.$refs[`${prefix}Ref`].getInfo(code);
      }
      this[`model${prefix}Flag`] = true;
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "合计";
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.sum(data, column.property);
          }
          return null;
        })
      ];
    }
  }
};
</script>
